/**
 * Magic dtree 2.5.1
 * Author: Mac_J@boarsoft.com
 * need: dynatree.js
 */
mac.dtree = function(self, cfg) {
	self = $.extend(self, cfg);
	self.rootNode = null;
	self.setContextMenu = function(span) {
		if(!self.ctxMenu || self.ctxMenu.length!=1)
			return;
//		var df = self.find('.ui-dynatree-document, .ui-dynatree-folder');
		var df = self.find('.dynatree-node, .dynatree-folder');
		//var df = $(span);
		df.contextMenu({
			menu : self.ctxMenu,
			onPopup: self.onCtxMenu,
			offset: self.menuOffset
		}, function(act, el, pos) {
			var dt = $.ui.dynatree.getNode(el);
			//var dt = el.context.dtnode;
			//var dt = el.prop('dtnode');
			dt.activate(true);
			self.onCtxMIClick[act](self, dt, el, pos);
		});
	};
	self.addNode = function(dd){
		var nn, pn = self.getNode(dd.parent);
		if (!pn)
			return;
		pn.data.isFolder = true;
		pn.data.childCount++;
		nn = pn.addChild(dd);
		pn.expand(true);
		self.setContextMenu();
		return nn;
	}
	self.updateNode = function(dd){
		var dt = self.getNode(dd.id || dd.key);
		dt.data = dd;
		dt.render();
		self.setContextMenu();
		return dt;
	}
	self.deleteNode = function(key){
		var pn = self.getNode(key);
		if(!pn)
			return;
		var pt = self.getNode(pn.data.parent);
		pt.data.childCount--;
		if (pt.data.childCount == 0) {
			pt.data.isFolder = false;
			pt.data.isLazy = false;
		}
		pn.remove();
	}
	self.deleteChildren = function(dt) {
		dt.visit(function(n){
			n.remove();
		});
		dt.data.isFolder = false;
		dt.data.isLazy = false;
		dt.data.childCount = 0;
	}
	self.expandNode = function(dt, dd) {
		if(self.beforeLoad) {
			self.beforeLoad(dt, dd);
		}
		dt.removeChildren();
		dt.addChild(dd);
		dt.expand();
		self.setContextMenu();
		if(self.afterLoad) {
			self.afterLoad(dt, dd);
		}
	}
	self.loadSubTree = function(dt) {
		var p = $.extend(self.loader.params || {}, {
			id: dt.data.key
		});
		$.post(self.loader.url, p, function(rs) {
			dt.setLazyNodeStatus(DTNodeStatus_Ok);
			var ro = mac.eval(rs);
			if (!ro.success) {
				dt.setLazyNodeStatus(DTNodeStatus_Error);
				return;
			}
			self.expandNode(dt, ro.data);
		});
	};
	self.treeConfig = $.extend(true, {
		fx : {
			height : 'toggle',
			duration : 200
		},
		onLazyRead : function(dt) {
			self.loadSubTree(dt);
		},
		onClick : function(dt, event) {
			var a = self.ctxMenu;
			if (a && a.is(':visible') && a.css('display')!='none')
				return false;
			return true;
		},
		onActivate : function(dt) {
			if(self.onActivate)
				self.onActivate(dt);
		}
	}, self.treeConfig);
	self.dynatree(self.treeConfig);
	//
	self.getTree = function(){
		return self.dynatree('getTree');
	}
	self.getNode = function(k){
		return self.getTree().getNodeByKey(k);
	}
	self.rootNode = self.dynatree('getRoot');
	//
	self.expand2 = function(p, nf){
		p = '/_1' + p;
		self.getTree().loadKeyPath(p, function(dt, st){
			if (st == "loaded") {
				dt.expand();
			} else if (st == "ok") {
				dt.activate();
				dt.expand();
			} else if (st == "notfound") {
				//self.rootNode.expand();
				nf && nf(p);
			}
		});
	}
	return self;
};